package com.milhouz.bomberrokon.sprite;

import java.util.List;

import com.milhouz.bomberrokon.model.Tile;
import com.stickycoding.Rokon.Rokon;
import com.stickycoding.Rokon.Texture;

public class ExplosionSprite {

	public int explosionId;

	private int explosionForce;

	public ExplosionTileSprite[] explosionTiles;

	public ExplosionSprite(int id, BombSprite bomb, List<Tile> tilesExplosed, Texture[] explosionTextures, int layer) {
		explosionId = id;
		explosionForce = bomb.getBombForce();
		int index = 0;
		// add the center explosion
		ExplosionTileSprite explosedTile = new ExplosionTileSprite(bomb.getX() //
				, bomb.getY() //
				, getExplosionTexture(bomb, bomb.getX(), bomb.getY(), false, explosionTextures));
		explosionTiles = new ExplosionTileSprite[tilesExplosed.size() + 1];
		explosionTiles[index++] = explosedTile;
		Rokon.getRokon().addSprite(explosedTile, layer);
		// add around explosion
		for (Tile tile : tilesExplosed) {
			explosedTile = new ExplosionTileSprite(tile //
					, getExplosionTexture(bomb, tile.getTileX(), tile.getTileY(), tile.isExplosionBound(), explosionTextures));
			explosionTiles[index++] = explosedTile;
			Rokon.getRokon().addSprite(explosedTile, layer);
		}
	}

	/** Return the explosion texture corresponding placement corresponding to the bomb coordonates */
	private Texture getExplosionTexture(BombSprite bomb, float x, float y, boolean bound, Texture[] bombTextures) {
		if (x > bomb.getX() && bound) {
			return bombTextures[7]; // right bound explose texture
		} else if (x > bomb.getX() && !bound) {
			return bombTextures[6]; // right explose texture
		} else if (x < bomb.getX() && bound) {
			return bombTextures[5]; // left bound explose texture
		} else if (x < bomb.getX() && !bound) {
			return bombTextures[4]; // left explose texture
		} else if (y > bomb.getY() && bound) {
			return bombTextures[3]; // bottom bound explose texture
		} else if (y > bomb.getY() && !bound) {
			return bombTextures[2]; // bottom explose texture
		} else if (y < bomb.getY() && bound) {
			return bombTextures[1]; // up bound explose texture
		} else if (y < bomb.getY() && !bound) {
			return bombTextures[0]; // up explose texture
		} else {
			return bombTextures[8]; // center explose texture
		}
	}

	/** Indicate when the explosion is finished and remove it from rokon engine */
	public boolean isFinished() {
		for (ExplosionTileSprite tile : explosionTiles) {
			if (!tile.isFinished()) {
				return false;
			} else {
				tile.markForRemoval();
			}
		}
		return true;
	}

	/** Return center X of the explosion */
	public float getX() {
		return explosionTiles[0].getX();
	}

	/** Return center Y of the explosion */
	public float getY() {
		return explosionTiles[0].getY();
	}

	/** Return the explosion id */
	public int getExplosionId() {
		return explosionId;
	}

	/** Return the explosion tiles sprites */
	public ExplosionTileSprite[] getExplosionTiles() {
		return explosionTiles;
	}

	public int getExplosionForce() {
		return explosionForce;
	}

}
